const toolTree = {
  /**
   * 树形结构转换为列表结构 - 子节点字段默认 children
   * @param {树状数据} tree
   * @returns 列表数据
   */
  treeToList(tree = [], children = "children") {
    let list = [];
    tree.forEach((item) => {
      list = [...list, ...toolTree.treeToList(item[children], children)];
    });
    return [...list, ...tree];
  },
  /**
   * 列表数据转换为树状数据
   * @param {列表数据} list
   * @param {关键字} key
   */
  listToTree(list = [], pid = "pid", id = "id") {
    // 获取根list，pid为0或者为空
    let rootList = list.filter((item) => !item[pid]);
    list.forEach((item) => {
      item.children = list.filter((_item) => _item[pid] == item[id]);
    });
    return rootList;
  },
};

export default toolTree;
